package com.topisv.tms.web.stock.instorage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springside.modules.web.Servlets;
import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.dto.OutstorageInfoDto;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.stock.instorage.OutstorageInfoService;

/**
 * 出库信息
 * @author ch
 *
 */
@Controller
@RequestMapping(value = "/outstorageInfo")
public class OutstorageInfoController {

	private static final Logger logger = LoggerFactory.getLogger(OutstorageInfoController.class);

	@Autowired
	private CustomersService customersService;
	@Autowired
	private AreaGroupService areaGroupService;
	@Autowired
	private OutstorageInfoService outstorageInfoService;
	
	
	//导向出库信息页面
	@RequestMapping(value = "toOutstorageInfoPage")
	public String toOutstorageInfoPage(Model model, ServletRequest request) {
		try {
			//当前用户关联的商家列表
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			List<Customers> customerList=this.customersService.loadCustomerByAuthority(curUser);
			model.addAttribute("customerList",customerList);
			model.addAttribute("userType",curUser.userType);
			
			//如果是商家用户,初始化数据
			if("customer".equals(curUser.userType)){
				String customerCode=curUser.customerCode;
			    //初始化仓库数据
				if(StringUtils.isNotBlank(customerCode)){
					List<AreaGroup> warehouseList=this.areaGroupService.loadAreaGroupByAuthority(curUser,customerCode);
					model.addAttribute("warehouseList", warehouseList);
					model.addAttribute("_customerCode", customerCode);
				}
			}
			
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		return "instorage/outstorageInfo";
	}
		
	//查询出库信息
	@RequestMapping(value = "queryOutstorageInfo")
	public String queryOutstorageInfo(Model model, ServletRequest request) {
		try {
			
			//当前用户关联的商家列表
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			List<Customers> customerList=this.customersService.loadCustomerByAuthority(curUser);
			model.addAttribute("customerList",customerList);
			model.addAttribute("userType",curUser.userType);
			
			Map<String, Object> param = Servlets.getParametersStartingWith(request, "_");
			String warehouseCode=(String)param.get("warehouseCode");
			String batchCode=(String)param.get("batchCode");
			String taskCode=(String)param.get("taskCode");
			String sku=(String)param.get("sku");
			
			//时间筛选
			String fromDateStr=(String) param.get("fromDate");
			String toDateStr=(String) param.get("toDate");
			Date fromDate=null;
			Date toDate=null;
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(StringUtils.isNotEmpty(fromDateStr)){
				fromDate=sdf.parse(fromDateStr+" 00:00:00");
			}
			if(StringUtils.isNotEmpty(toDateStr)){
				toDate=sdf.parse(toDateStr+" 23:59:59");
			}
			param.put("fromDate", fromDate);
			param.put("toDate", toDate);
			
			//初始化仓库数据
			String customerCode=(String)param.get("customerCode");
			List<AreaGroup> warehouseList=this.areaGroupService.loadAreaGroupByAuthority(curUser, customerCode);
			model.addAttribute("warehouseList", warehouseList); 
			
			//出库信息
			List<OutstorageInfoDto> dtoList = this.outstorageInfoService.loadDtoByParam(param);
			model.addAttribute("dtoList", dtoList);
			
			model.addAttribute("_customerCode", customerCode);
			model.addAttribute("_warehouseCode", warehouseCode);
			model.addAttribute("_batchCode", batchCode);
			model.addAttribute("_taskCode", taskCode);
			model.addAttribute("_sku", sku);
			model.addAttribute("_fromDate", fromDateStr);
			model.addAttribute("_toDate", toDateStr);
			  
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		
		return "instorage/outstorageInfo";
	}
	
}
